package se.webbzon.boltzmann.game.event;

import se.webbzon.boltzmann.game.object.spawner.AbstractSpawner;
import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldobject.WorldObject;

public class SpawnEvent extends AbstractGameEvent {
	
	// The sequence associated with this event
	private final Sequence sequence;
	
	// The binding of this event
	private final WorldObject binding;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SpawnEvent(Sequence sequence, WorldObject binding, String[] arguments) {
		super(arguments);
		this.sequence = sequence;
		this.binding = binding;
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override protected void onBegin(WorldArea world) {
		if (binding instanceof AbstractSpawner) {
			AbstractSpawner spawner = (AbstractSpawner) binding;
			WorldObject instance = spawner.spawn(sequence.getEnvironment());
			world.createInstance(instance);
			sequence.bindTo(getArgument(0), instance);
		}
		end();
	}

	@Override protected void onEnd() {};

}
